<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }

      .content {
        width: 1200px;
        background: yellow;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
      }

      .content > div {
        width: 320px;
        height: 300px;
        background: green;
        margin-top: 50px;
      }
    </style>
  </head>

  <body>
    <div class="content">
      <!-- <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div> -->
    </div>
    <script>
      /*
            实现原理：
            当前总高度（滚动+可视） - 当前滚动宽度 === 当前可视高度
        */
      window.onload = function () {
        let content = document.getElementsByClassName('content')[0];
        for (let i = 0; i < 9; i++) {
          const _Div = document.createElement('div');
          _Div.innerHTML = i;
          _Div.style.background = bgColor();
          content.appendChild(_Div);
        }
        window.onscroll = function () {
          let scrollTop = document.documentElement.scrollTop; //当前滚动的高度
          let clientHeight = document.documentElement.clientHeight; //当前可视的高度
          let bodyHeight = document.body.clientHeight; //当前高度包括滚动条
          if (parseInt(bodyHeight - scrollTop) == clientHeight) {
            for (let i = 0; i < 9; i++) {
              const _Div = document.createElement('div');
              _Div.innerHTML = i;
              _Div.style.background = bgColor();
              content.appendChild(_Div);
            }
          }
        };
        function bgColor() {
          const r = Math.round(Math.random() * 255);
          const g = Math.round(Math.random() * 255);
          const b = Math.round(Math.random() * 255);
          return `rgb(${r},${g},${b})`;
        }
      };
    </script>
  </body>
</html>
